from django.http import HttpResponse
from django.shortcuts import render, redirect,reverse

from .forms import *
from .models import CustomUser
from django.contrib.auth import login as lgi, logout as lgo
from django.contrib.auth.decorators import login_required


def logout(request):
    lgo(request)
    return redirect(reverse('user:login'))

def center(request):
    if request.user.is_authenticated:
        # 调试信息
        print(f"用户: {request.user.username}")
        print(f"头像字段: {request.user.header}")
        if request.user.header:
            print(f"头像URL: {request.user.header.url}")
        else:
            print("用户没有设置头像")
        return render(request, 'user/center.html')
    else:
        return redirect(reverse('user:login'))


def forgetpwd(request):
    return HttpResponse("忘记密码")

@login_required
def changeinfo(request):
    if request.method == 'POST':
        cif = ChangeInfoForm(data=request.POST, files=request.FILES, instance=request.user)
        if cif.is_valid():
            cif.save()
            return redirect(reverse('user:center'))
        else:
            return render(request, "user/changeinfo.html", {'form': cif})
    else:
        cif = ChangeInfoForm(instance=request.user)
        return render(request, "user/changeinfo.html", {'form': cif})

def change_pwd(request):
    if request.user.is_authenticated:
        if request.method == 'POST':
            form = ChangePwdForm(request.POST)
            if form.is_valid():
                username = request.user.username
                user = CustomUser.objects.get(username=username)
                if user.check_password(form.cleaned_data['password']):
                    user.set_password(form.cleaned_data['password1'])
                    user.save()
                    lgo(request)
                    return redirect(reverse('user:login'))
                else:
                    form.add_error('password', "原密码输入错误！")
                    return render(request, "user/changepwd.html", {'form': form})
            else:
                return render(request, "user/changepwd.html", {'form': form})
        else:
            cf = ChangePwdForm()
            return render(request, "user/changepwd.html", {'form': cf})


    else:
        return redirect(reverse('user:login'))



def login(request):
    if request.method == "POST":
        form = LoginForm(request.POST)
        if form.is_valid():
            user = CustomUser.objects.filter(username=form.cleaned_data['username']).first()
            if user:
                if user.check_password(form.cleaned_data['password']):
                    lgi(request, user)
                    return redirect('/main/home/')
                else:
                    form.add_error('password', '密码错误')
                    return render(request, "user/login.html", {'form': form})
            else:
                form.add_error('username', '用户不存在')
                return render(request, "user/login.html", {'form': form})
        else:
            return render(request, "user/login.html", {'form': form})
    else:
        lf = LoginForm()
        return render(request, "user/login.html",{'form':lf})





def regist(request):
    if request.method == "POST":
        form = RegisForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.set_password(form.cleaned_data['password'])
            user.is_superuser = True
            user.save()
            return redirect(reverse('user:login'))
        else:
            return render(request, "user/regist.html", {'form': form})
    else:
        rf = RegisForm()
        return render(request, "user/regist.html",{'form':rf})

@login_required
def changehead(request):
    if request.method == 'POST':
        form = ChangeHeadForm(data=request.POST, files=request.FILES, instance=request.user)
        if form.is_valid():
            form.save()
            return redirect(reverse('user:center'))
        else:
            return render(request, 'user/changehead.html', {'form': form})
    else:
        form = ChangeHeadForm(instance=request.user)
        return render(request, 'user/changehead.html', {'form': form})




